package com.click369.mianshiqingbao.service.impl;

import com.click369.mianshiqingbao.mapper.ReadLogBeanMapper;
import com.click369.mianshiqingbao.mapper.StoreBeanMapper;
import com.click369.mianshiqingbao.mapper.TopicBeanMapper;
import com.click369.mianshiqingbao.pojo.*;
import com.click369.mianshiqingbao.pojo.TopicBeanExample.Criteria;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.pojo.entity.TagTypeBean;
import com.click369.mianshiqingbao.pojo.entity.TopicTypeNumBean;
import com.click369.mianshiqingbao.service.TopicService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * 题目信息的业务访问接口实现类
 */
@Service
public class TopicServiceImpl implements TopicService{
    @Autowired
    private TopicBeanMapper topicBeanMapper;
    @Autowired
    private ReadLogBeanMapper readLogBeanMapper;
    @Autowired
    private StoreBeanMapper storeBeanMapper;
    /**
     * 条件分页查询题目信息列表
     * @return 分页查询信息的封装类对象
     */
    @Override
    public PageResult findPage(TopicBean topicbean, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<TopicBean> page=(Page<TopicBean>)topicBeanMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 增加题目信息
     * @param topicbean 题目信息对象
     */
    @Override
    public void add(TopicBean topicbean) {
        topicBeanMapper.insert(topicbean);
    }
    /**
     * 根据ID获取题目信息对象
     * @param id 题目信息的id
     * @return 题目信息对象
     */
    @Override
    public TopicBean findOne(Long id) {
        return topicBeanMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改题目信息
     * @param topicbean 题目信息对象
     */
    @Override
    public void update(TopicBean topicbean) {
        topicBeanMapper.updateByPrimaryKey(topicbean);
    }
    /**
     * 批量删除题目信息
     * @param ids 批量删除题目信息的id集合
     */
    @Override
    public void delete(Long[] ids) {
        for(long id:ids){
            topicBeanMapper.deleteByPrimaryKey(id);
        }
    }
    /**
     * 获取所选择的一级分类对应的阅读次数最多的题目列表【10】
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 阅读次数最多的题目列表
     */
    @Override
    public List<TopicBean> getMostTopic(Integer classifyId) {
        TopicBeanExample example = new TopicBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTopicClassifyOneEqualTo(classifyId);
        criteria.andTopicCheckedStatusEqualTo("3");
        example.setOrderByClause("topic_freq");
        List<TopicBean> topiclist = topicBeanMapper.selectByExample(example);
        Collections.reverse(topiclist);
        if(topiclist.size()>5){
            topiclist = topiclist.subList(0,5
            );
        }
        return topiclist;
    }
    /**
     * 获取所选择的一级分类对应的最新题目列表【10】
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 最新的题目列表
     */
    @Override
    public List<TopicBean> getNewTopic(Integer classifyId) {
        TopicBeanExample example = new TopicBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTopicClassifyOneEqualTo(classifyId);
        criteria.andTopicCheckedStatusEqualTo("3");
        example.setOrderByClause("topic_create_time");
        List<TopicBean> topiclist = topicBeanMapper.selectByExample(example);
        Collections.reverse(topiclist);
        if(topiclist.size()>10){
            topiclist = topiclist.subList(0,10);
        }
        return topiclist;
    }
    /**
     * 获取所选择的一级分类指定题型题目的列表
     * @param classifyId 当前用户所选择的一级分类ID
     * @param topicType 题目类型对应编号 ["1-单选","2-多选","3-判断","4-填空","5-问答"]
     * @return 指定题型题目的列表
     */
    @Override
    public List<TopicBean> getTopicBeanList(Integer classifyId, String topicType) {
        TopicBeanExample example = new TopicBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTopicClassifyOneEqualTo(classifyId);
        criteria.andTopicTypeEqualTo(topicType);
        criteria.andTopicCheckedStatusEqualTo("3");
        List<TopicBean> topiclist = topicBeanMapper.selectByExample(example);
        return topiclist;
    }
    /**
     * 获取所选择的一级分类对应的各个题型题目的数量
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 题目类型和个数的集合
     */
    @Override
    public List<TopicTypeNumBean> getTopicTypeNumList(Integer classifyId) {
        List<TopicTypeNumBean> topicTypeNumBeanList = new ArrayList<TopicTypeNumBean>();
        String topicType[] = {"1-单选","2-多选","3-判断","4-填空","5-问答"};
        for(String str:topicType){
            String strarray[] = str.split("-");
            int num = getTopicBeanList(classifyId,strarray[0]).size();
            TopicTypeNumBean topicTypeNumBean = new TopicTypeNumBean();
            topicTypeNumBean.setTopictypeid(Integer.parseInt(strarray[0]));
            topicTypeNumBean.setTopictypename(strarray[1]);
            topicTypeNumBean.setTopictypenum(num);
            topicTypeNumBeanList.add(topicTypeNumBean);
        }
        return topicTypeNumBeanList;
    }
    /**
     * 得到当前所选择的一级分类的题目总数
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 一级分类的题目总数
     */
    public int getTopicCountByClassifyId(Integer classifyId) {
        TopicBeanExample example = new TopicBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTopicClassifyOneEqualTo(classifyId);
        criteria.andTopicCheckedStatusEqualTo("3");
        return topicBeanMapper.selectByExample(example).size();
    }
    /**
     * 根据当前用户id所选择的一级分类得到已读题目列表
     * @param userId 当前用户id
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 已读题目列表
     */
    @Override
    public List<TopicBean> getReadTopicList(Long userId, Integer classifyId){
        //1. 根据当前用户id得到该用户的阅读记录
        ReadLogBeanExample example = new ReadLogBeanExample();
        ReadLogBeanExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTopicClassifyOneEqualTo(classifyId);
        List<ReadLogBean> readloglist = readLogBeanMapper.selectByExample(example);
        //2. 根据获取到的阅读记录对象中的topicId获取题目对象
        List<TopicBean> topicBeanList = new ArrayList<TopicBean>();
        if(readloglist.size()>0){
            for(ReadLogBean readLogBean:readloglist){
                TopicBean topicBean = topicBeanMapper.selectByPrimaryKey(readLogBean.getTopicId());
                topicBeanList.add(topicBean);
            }
        }
        return topicBeanList;
    }

    /**
     * 获取所选择的一级分类指定阅读状态题目列表
     * @param userId 当前用户id
     * @param classifyId 当前用户所选择的一级分类ID
     * @param tagFlag 指定阅读状态 {"1-了解","2-掌握","3-熟练","4-精通"};
     * @return 指定阅读状态题目列表
     */
    @Override
    public List<TopicBean> getReadTagTopicList(Long userId,Integer classifyId,String tagFlag){
        ReadLogBeanExample example = new ReadLogBeanExample();
        ReadLogBeanExample.Criteria criteria=example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTopicClassifyOneEqualTo(classifyId);
        criteria.andReadTopicStatusEqualTo(tagFlag);
        List<ReadLogBean> readlist = readLogBeanMapper.selectByExample(example);
        List<TopicBean> topicBeanList = new ArrayList<TopicBean>();
        if(readlist.size()>0){
            for (ReadLogBean readLogBean:readlist){
                TopicBean topicBean = topicBeanMapper.selectByPrimaryKey(readLogBean.getTopicId());
                topicBeanList.add(topicBean);
            }
        }
        return topicBeanList;
    }
    /**
     * 根据当前用户id所选择的一级分类得到对应题目阅读标签的列表
     * @param userId 当前用户id
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 当前用户所选择的一级分类的阅读状态标签列表
     */
    public List<TagTypeBean> getReadTagTypeBeanList(Long userId,Integer classifyId){
        List<TagTypeBean> taglist = new ArrayList<TagTypeBean>();
        String tagType[] = {"1-了解","2-掌握","3-熟练","4-精通"};
        for(String str:tagType){
            String tagarray[] = str.split("-");
            TagTypeBean tagTypeBean = new TagTypeBean();
            tagTypeBean.setTagId(Integer.parseInt(tagarray[0]));
            tagTypeBean.setTagname(tagarray[1]);
            tagTypeBean.setTagNum(getReadTagTopicList(userId,classifyId,tagarray[0]).size());
            taglist.add(tagTypeBean);
        }
        return taglist;
    }
    /**
     * 获取当前用户所选择的一级分类得到收藏题目列表
     * @param userId 当前用户id
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 收藏题目列表
     */
    @Override
    public List<TopicBean> getStoreTopicList(Long userId, Integer classifyId) {
        StoreBeanExample example = new StoreBeanExample();
        StoreBeanExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<StoreBean> storeBeanList = storeBeanMapper.selectByExample(example);
        List<TopicBean> topicList = new ArrayList<TopicBean>();
        if(storeBeanList.size()>0){
            for(StoreBean storeBean:storeBeanList){
                TopicBean topicBean = topicBeanMapper.selectByPrimaryKey(storeBean.getTopicId());
                if(topicBean.getTopicClassifyOne()==classifyId){
                    topicList.add(topicBean);
                }
            }
        }
        return topicList;
    }

    /**
     * 获取当前用户所选择的一级分类的阅读标签列表
     * @param userId 当前用户id
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 当前用户所选择的一级分类的阅读标签列表
     */
    @Override
    public List<TagTypeBean> getTagTypeBeanList(Long userId, Integer classifyId) {
        List<TagTypeBean> finalTagList = new ArrayList<TagTypeBean>();
        //1.得到当前所选择的一级分类的题目总数
        int count = getTopicCountByClassifyId(classifyId);


        //3.根据当前用户id所选择的一级分类得到对应题目阅读标签的列表
        List<TagTypeBean> tagTypeBeanList = getReadTagTypeBeanList(userId,classifyId);
        for (TagTypeBean tag:tagTypeBeanList){
            tag.setTagCount(count);
            tag.setTagId(finalTagList.size()+1);
            finalTagList.add(tag);
        }
        //2.根据当前用户id所选择的一级分类得到已读题目列表
        List<TopicBean> topicBeanList = getReadTopicList(userId,classifyId);
        TagTypeBean readTag = new TagTypeBean();
        readTag.setTagId(finalTagList.size()+1);
        readTag.setTagname("已读");
        readTag.setTagNum(topicBeanList.size());
        readTag.setTagCount(count);
        finalTagList.add(readTag);
        //4.得到当前用户id的收藏题目列表
        List<TopicBean> storeTopicList=getStoreTopicList(userId,classifyId);
        //封装已读状态的标签
        TagTypeBean StoreTag=new TagTypeBean();
        StoreTag.setTagId(finalTagList.size()+1);
        StoreTag.setTagname("收藏");
        StoreTag.setTagNum(storeTopicList.size());
        StoreTag.setTagCount(count);
        //将封装好的已读状态的标签添加到阅读标签列表
        finalTagList.add(StoreTag);
        return finalTagList;
    }

    /**
     * 获取当前用户Id和题目Id查询一个题目对象
     * @param topicId 题目Id
     * @return 题目对象
     */
    @Override
    public TopicBean getTopicObject(Long topicId) {
        //得到题目对象
        TopicBean topicBean = topicBeanMapper.selectByPrimaryKey(topicId);
        return topicBean;
    }
}
